home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Demos / SAT Invaders demo ƒ / StarField.c < prev    next >
Text File  |  1995-11-09  |  2KB  |  93 lines

  1. /* Starfield unit for SAT Invaders.
  2. This unit uses the pixel array operations in SAT.
  3. Richard Bannister deserves credits for making a similar improved SAT Invaders,
  4. though using sprites for the stars. His demo convinced me that there should be
  5. single pixel operations in SAT. */
  6.  
  7. #include <SAT.h>
  8. #include <SATAddOnLib.h>
  9.  
  10.  void InitStars();
  11.  void DoStars();
  12.  void ToggleStarField (Boolean starFieldFlag);
  13.  
  14.  
  15.  PixelPtr px=nil, pxcopy;
  16.  
  17. #define kNumPixels 100
  18. #define Abs(x) (x>0?x:-x)
  19.  
  20.  void InitStars()
  21. {
  22.   short i;
  23.  
  24.   px = (PixelPtr)NewPtrClear(sizeof(Pixel) * kNumPixels);
  25.   pxcopy = (PixelPtr)NewPtrClear(sizeof(Pixel) * kNumPixels);
  26.   for (i = 0; i< kNumPixels; i++)
  27.    {
  28.     px[i].data1 = 0;
  29.     px[i].data2 = SATRand(7) + 1;
  30.     px[i].position.h = SATRand(gSAT.offSizeH);
  31.     px[i].position.v = SATRand(gSAT.offSizeV);
  32.    }
  33. } //InitStars
  34.  
  35. void ToggleStarField (Boolean starFieldFlag)
  36. {
  37. SATPort   savePort;
  38.  
  39.   if (starFieldFlag) {
  40.     SATGetPort(&savePort);
  41.     SATSetPortBackScreen();
  42.     ForeColor(blackColor);
  43.     PaintRect(&gSAT.backScreen.bounds);
  44.     CopyBits(&gSAT.backScreen.port->portBits, &gSAT.offScreen.port->portBits, &gSAT.backScreen.bounds, &gSAT.backScreen.bounds, srcCopy, nil);
  45.     SATRedraw();
  46.     SATSetPort(&savePort);
  47.    }
  48.   else
  49.    {
  50.     SATDrawPICTs(129, 128);
  51.     SATRedraw();
  52.    };
  53. } //{ToggleStarField}
  54.  
  55. //The pixel set is processed with the assumption that all pixels move every frame.}
  56. //They are drawn on top of the sprites.}
  57.  
  58.  void DoStars()
  59. {
  60.   short i;
  61.  
  62.   if (px == nil)
  63.    InitStars();
  64.   BlockMove((Ptr)px, (Ptr)pxcopy, GetPtrSize((Ptr)px));        //{Make a copy}
  65.   for (i = 0; i< kNumPixels; i++)
  66.    {
  67.     px[i].position.h = px[i].position.h + px[i].data1;
  68.     px[i].position.v = px[i].position.v + px[i].data2;
  69.  
  70.     if (px[i].position.h < 0)
  71.      {
  72.       px[i].position.h = 0;
  73.       px[i].data1 = Abs(px[i].data1);
  74.      };
  75.     if (px[i].position.v < 0)
  76.      {
  77.       px[i].position.v = 0;
  78.       px[i].data2 = Abs(px[i].data2);
  79.      };
  80.     if (px[i].position.h >= gSAT.offSizeH)
  81.      {
  82.       px[i].position.h = gSAT.offSizeH - 1;
  83.       px[i].data1 = -Abs(px[i].data1);
  84.      };
  85.     if (px[i].position.v >= gSAT.offSizeV)
  86.      {
  87.       px[i].position.v = 0;
  88.      };
  89.    };
  90.   SATDrawPixels(px, &gSAT.wind, 0xffffff00);                //{Draw in new positions}
  91.   SATCopyPixels(pxcopy, &gSAT.offScreen, &gSAT.wind);        //{Erase old positions}
  92.  } //{DoStars}
  93.